package cn.edu.ctbu.api01.service;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.exception.REnum;
import cn.edu.ctbu.api01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    /**
     * 分页取所有学生
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }
    /**
     * 按id进行查询
     * @param id  主键，整数
     * @return
     */
    public Student findById(Integer id) {
        return studentRepository.findById(id).orElse(null);
    }
    /**
     * 按姓名进行查询  like
     * @param name  姓名
     * @return
     */
    public List<Student> findByName(String name) {
        return studentRepository.findByNameLike(name);
    }
    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        studentRepository.deleteById(id);
    }
    /**
     * 删除
     * @param student
     */
    public void delete(Student student) {
        studentRepository.delete(student);
    }
    /**
     * 插入即新增
     * @param student
     */
    public Student insert(Student student) {
        return studentRepository.save(student);
    }
    /**
     * 更新即更改
     * @param student
     */
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    /**
     * 多种查询学生方式
     * @param no
     * @param name
     * @param sex
     * @param ageRange
     * @param scoreRange
     * @return
     */
    public List<Student> searchStudents(String no, String name, Integer sex, String ageRange, String scoreRange) {
        if (no != null) {
            return studentRepository.findByNo(no);
        } else if (name != null) {
            return studentRepository.findByNameContaining(name);
        } else if (sex != null) {
            return studentRepository.findBySex(sex);
        } else if (ageRange != null) {
            String[] ages = ageRange.split("-");
            int minAge = Integer.parseInt(ages[0]);
            int maxAge = Integer.parseInt(ages[1]);
            return studentRepository.findByAgeBetween(minAge, maxAge);
        } else if (scoreRange != null) {
            String[] scores = scoreRange.split("-");
            int minScore = Integer.parseInt(scores[0]);
            int maxScore = Integer.parseInt(scores[1]);
            return studentRepository.findByScoreBetween(minScore, maxScore);
        } else {
            return studentRepository.findAll();
        }
    }

    /**
     * 验证用户名和密码
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    public Boolean validUserNameAndPassword(String username, String password) throws Exception {
        List<Student> students= studentRepository.findByNameAndPassword(username, password);
        if (students.size()>0) { //!students.isEmpty()
            //找到用户
            return true;
        }
        else{
            throw new Exception("出错了，不正确的用户名和密码！！！");
//            throw new Exception(REnum.LOGIN_ERR);
        }
    }
}
